using App.Modelo.Interfaces;
using NHibernate;
using NHibernate.Type;

namespace App.Persistencia
{
    internal class Interceptor : EmptyInterceptor
    {
        InterceptorNotifier notify;

        public Interceptor(InterceptorNotifier NotifyDelegate)
        {
            notify = NotifyDelegate;
        }

        public override void OnDelete(
            object entity,
            object id,
            object[] state,
            string[] propertyNames,
            IType[] types )
        {
            notify(TriggerActionType.Delete, entity, id, state, null, propertyNames, types); 
            base.OnDelete(entity, id, state, propertyNames, types);
        }

        public override bool OnSave(
            object entity, 
            object id, 
            object[] state, 
            string[] propertyNames, 
            IType[] types)
        {
            notify(TriggerActionType.Create, entity, id, state, null, propertyNames, types); 
            return base.OnSave(entity, id, state, propertyNames, types);
        }

        public override bool OnFlushDirty(
            object entity,
            object id,
            object[] currentState,
            object[] previousState,
            string[] propertyNames,
            IType[] types)
        {
            notify(TriggerActionType.Update, entity, id, currentState, previousState, propertyNames, types); 
            return base.OnFlushDirty(entity, id, currentState, previousState, propertyNames, types);
        }
        
        
    }
}